#!/bin/sh

die () { echo "$@" ; cleanup ; exit 1; }

cleanup() {
  unset -f nvm_download nvm_ls_remote nvm_ls_remote_iojs
  if [ -n "${TEMP_NVM_COLORS-}" ]; then
    export NVM_COLORS=TEMP_NVM_COLORS
  fi
  unset TEMP_NVM_COLORS
  rm -rf "${NVM_DIR}/alias/lts"
  mv "${NVM_DIR}/alias/lts-backup" "${NVM_DIR}/alias/lts"
}

\. ../../../nvm.sh
if [ -n "${NVM_COLORS-}" ]; then
  export TEMP_NVM_COLORS=NVM_COLORS
  unset NVM_COLORS
fi

MOCKS_DIR="../Unit tests/mocks"

LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"

STAR="$(cat "${MOCKS_DIR}/lts-star.txt")"

mv "${NVM_DIR}/alias/lts" "${NVM_DIR}/alias/lts-backup" ||:
mkdir -p "${NVM_DIR}/alias/lts"

echo "${STAR}" > "${NVM_DIR}/alias/lts/\*"
printf '%s\n' "$(cat "${LTS_NAMES_PATH}" | tail -n +1)" | while IFS= read -r LTS; do
  cp "${NVM_DIR}/alias/lts-backup/${LTS}" "${NVM_DIR}/alias/lts/"
done

nvm deactivate 2>/dev/null || die 'unable to deactivate'

\. ../../common.sh

nvm_download() {
  if [ "$*" = "-L -s $(nvm_get_mirror node std)/index.tab -o -" ]; then
    cat "${MOCKS_DIR}/nodejs.org-dist-index.tab"
    return
  fi
  return 42
}

EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote lts.txt"
OUTPUT="$(nvm ls-remote --lts | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote --lts did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote lts.txt"
OUTPUT="$(nvm ls-remote "lts/*" | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote lts/* did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"

INDEX=1
printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do
  ACTUAL="$(nvm ls-remote "lts/-${INDEX}" | sed 's/[ \t]*$//')"
  MESSAGE="for lts/-${INDEX} (${LTS})"
  EXPECTED="$(nvm ls-remote "lts/${LTS}" | sed 's/[ \t]*$//')"
  [ "${ACTUAL}" = "${EXPECTED}" ] || die "${MESSAGE}: expected >${EXPECTED}<, got >${ACTUAL}<"

  INDEX=$(($INDEX + 1))
done

OUTPUT="$(nvm ls-remote lts/ARGON 2>&1)"
EXIT_CODE=$?
[ $EXIT_CODE -eq 3 ] || die "nvm ls-remote lts/ARGON did not exit 3, got '${EXIT_CODE}'"

EXPECTED_OUTPUT="LTS names must be lowercase
            N/A"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote lts/ARGON did not output expected error message; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

REMOTE="${PWD}/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
  cat "${REMOTE}"
}
REMOTE_IOJS="${PWD}/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
  cat "${REMOTE_IOJS}"
}

EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote.txt"
OUTPUT="$(nvm ls-remote | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "bare nvm ls-remote did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

cleanup
